package com.example.train_operate.util;

import lombok.Data;

import java.io.Serializable;
@Data
public class Result<T> implements Serializable {
    //状态码
    private Integer code;
    //返回信息
    private String message;

    private T data;

    //封装返回的数据
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum){
        Result<T> result = new Result<>();
        if(body != null){
            result.setData(body);
        }
        //状态码
        result.setCode(resultCodeEnum.getCode());
        //返回相关数据
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    //成功,但里面没数据
    public static <T> Result<T> ok(){
        return build(null, ResultCodeEnum.SUCCESS);
    }

    //成功，里面有数据
    public static <T> Result<T> ok(T data){
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok(T data, ResultCodeEnum resultCodeEnum){
        return build(data, resultCodeEnum);
    }

    //失败,但失败里面没有数据
    public static <T> Result<T> fail(){
        return build(null, ResultCodeEnum.FAIL);
    }

    //失败，里面有数据
    public static <T> Result<T> fail(T data){
        return build(data, ResultCodeEnum.FAIL);
    }

    public static <T> Result<T> fail(Integer code, String message){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    public static <T> Result<T> fail(T data, ResultCodeEnum dataError){
        return build(data, dataError);
    }

    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }


}
